package it.tukano.concurrent;

/**
 * A synchronized value wrapper
 * @author pgi
 * @param <T>
 */
public final class SyncVar<T> {

    /**
     * Create a new SyncVar with a default value
     * @param <A> the type of the variable
     * @param value the initial value
     * @return a SyncVar holding the given value
     */
    public static <A> SyncVar<A> newInstance(A value) {
        SyncVar<A> var = new SyncVar<A>();
        var.set(value);
        return var;
    }

    /** The value held by this var */
    private T value;

    /**
     * Instance initializer
     */
    public SyncVar() {
    }

    /**
     * Set the value held by this SyncVar
     * @param value the new value to set
     */
    public synchronized void set(T value) {
        this.value = value;
    }

    /**
     * Returns the value held by this SyncVar
     * @return the value of this syncvar
     */
    public synchronized T get() {
        return value;
    }

    /**
     * Checks if this var is empty
     * @return true if the value of this var is null
     */
    public synchronized boolean isEmpty() {
        return value == null;
    }
}
